package cn.szbo.jksystem.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.szbo.jksystem.core.GenericDaoImpl;
import cn.szbo.jksystem.dao.UserDao;
import cn.szbo.jksystem.model.User;
import cn.szbo.jksystem.util.UUIDUtil;

/**
 * 
 * @author nick xue
 * 
 *         2012-7-22 下午2:57:27
 */
@Repository
public class UserDaoImpl extends GenericDaoImpl<User> implements UserDao {

    public User selectUser(String loginId, String password) {
        String sqlStr = " select * from t_user where uname=? and password=? ";
        List<Object> paras = new ArrayList<Object>();
        paras.add(loginId);
        paras.add(password);
        Object o = this.queryForSingle(sqlStr, paras, new RowMapper() {
            public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                User user = new User();
                return user;
            }
        });
        if (null != o) {
            return (User) o;
        } else {
            return null;
        }
    }

    public List<Map<String, Object>> selectUsers() {
        String sqlStr = " select * from t_user u";
        List<Map<String, Object>> users = this.queryForList(sqlStr, null);
        return users;
    }

    public User selectUserByLoginId(String loginId) {
        String sqlStr = " select * from t_user where login_id=?";
        List<Object> paras = new ArrayList<Object>();
        paras.add(loginId);
        Object o = this.queryForSingle(sqlStr, paras, new RowMapper() {
            public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                User user = new User();
                return user;
            }
        });
        if (null != o) {
            return (User) o;
        } else {
            return null;
        }
    }

    public int insertUser(User user) {
        String sql = "insert into t_user(sid,login_id,password,email) values(?,?,?,?)";
        List<Object> paras = new ArrayList<Object>();
        paras.add(UUIDUtil.getUUID());
        return this.insert(sql, paras);
    }

}
